/*
 * TCSS 422 Web Spider Project
 * Group Money: Al McKenzie, Michael Pitts, Taylor Zielske
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;


/**
 * Displays the final results of the web search for the user.  This class may not be 
 * activated, based on the user's preference in the StartGUI.
 * 
 * @author Taylor Zielske
 * @version November 7, 2011
 */
public class FinishGUI{
	/**
	 * The frame
	 */
	private JFrame my_frame;
	/**
	 * The panel
	 */
	private JPanel my_panel;
	/**
	 * The string buffer
	 */
	private StringBuffer s;
	/**
	 * The text output
	 */
	private JTextPane output;
	/**
	 * Initializes the finish gui
	 */
	public FinishGUI() {
		my_frame = new JFrame("Web Crawler");
		my_panel = new JPanel();
		my_panel.setLayout(new BorderLayout());
		s = new StringBuffer();
		output = new JTextPane();
		output.setEditable(false);
		s = new StringBuffer();
	}

	/**
	 * Starts the final display GUI.
	 */
	public void start() {
		JPanel title = new JPanel( new BorderLayout());
		JLabel webcrawler = new JLabel("WEB CRAWLER - Results Summary");
		webcrawler.setFont(new Font("SansSerif", Font.BOLD, 14));
		webcrawler.setForeground(Color.DARK_GRAY);
		title.add(webcrawler);
		my_panel.add(title, BorderLayout.NORTH);
		my_panel.add(output, BorderLayout.CENTER);
		my_frame.add(my_panel);
	    my_frame.setSize(new Dimension(500,425));
	    my_frame.setTitle("Web Crawler");
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		my_frame.setVisible(true);
		my_frame.setResizable(false);
	}
	/**
	 * Is called when the SearchHopper has gotten data on a new web site. will only update gui with Search Hopper notifies
	 * the observers to stop
	 * @param the_arg is the data passed containing the search statistics.
	 */
	public void inputData(Object[] the_arg){
		s.append("Last Page Parsed: " + the_arg[0]+ "\n");

		s.append("Pages Retrieved: " + the_arg[1].toString() + "\n");
		s.append("Average words per page: " + 
				(float)(((Long)the_arg[2]) / ((Integer)the_arg[1])) + "\n");
		s.append("Average words per page: " + 
				((float)(((Long)the_arg[2])) / ((Integer)the_arg[1])) + "\n");
		s.append("Average URLs per page: " + 
				(((float)(((Integer)the_arg[3])) / ((Integer)the_arg[1]))) + "\n");
		s.append("Keyword\tAve. hits per page\t\tTotal hits\n");
		String[] words = (String[]) the_arg[6];
		int[] freq = (int[])the_arg[7];
		for (int i = 0; i < words.length; i++) {
			s.append(words[i]);
			s.append("\t");
			//if (words[i].length() < 7) {
				//s.append("\t");
			//}
			s.append((double)Math.round(((float) (freq[i]) / (Integer)the_arg[1])* 100000)/100000 + 
					"\t\t\t" + freq[i] + "\n");
		}
		s.append("\nPage limit: " + (Integer)the_arg[5] + "\n");
		s.append("Average parse time per page " + 
				(System.currentTimeMillis() - (Long)the_arg[4]) / (Integer)the_arg[1] + " msec" + "\n");
		s.append("Total running time: " + 
				(float) (System.currentTimeMillis() - (Long)the_arg[4]) / 1000 + " sec\n\n\n");
		output.setText(s.toString());
		output.repaint();
	}
}
